const app = getApp();
import {
  giftInfo,
  goodsList,
  giftsPage
} from "../../../server/api";
import { parseTime } from '../../../utils/util';
Page({
  data: {
    oss_url: app.globalData.oss_url,
    showquan:true,
    goodsList:[],
    active:1,
    haveORno:0,
    giftId:4,
    goodsList:[],
    giftsList:[],
    popupShow:false,
    giftIdMsg:{},
    getTime:'',   //领取时间
    giftTips:{}   //礼券提示
  },
  onLoad(options){
    this.setData({
      giftId:options.giftId
    })
    this.getGiftInfo();
   // this.getGiftsList();   礼券列表
    this.goodsLis();
  },
  onShow: function () {
    if(typeof this.getTabBar=== 'function' && this.getTabBar()){
      this.getTabBar().setData({
        active:1
      })
    }
    this.setData({
      haveORno:app.globalData.holdGifts
    })
  },
  //获取礼金包信息
  getGiftInfo(){
    let data={
      //this.data.giftId
      id:4
    }
    giftInfo(data).then(res=>{
      console.log(res)
      if(res.code==0){
        let getTime=parseTime(res.data.createTime,'{y}-{m}-{d} {h}:{i}');
        this.setData({
          giftIdMsg:res.data,
          getTime
        })
      }
    })
  },
  getGiftsList(){
    let oneId={
      oneId:app.globalData.user_info.oneId
    }
    giftsPage(oneId).then(res=>{
      if(res.code==0){
        let list=res.data.list;
        list.forEach(function(item){
          let getTime=parseTime(item.createTime,'{y}-{m}-{d} {h}:{i}');
          item.createTime=getTime;
        })
        that.setData({
          giftsList:list
        })
      }
    })
  },
  goodsLis(){
    let that=this;
     let data={
       oneId:1058
     }
     goodsList(data).then(res=>{
       if(res.code==0){
         let list=res.data.goodsInfoList;
         app.globalData.holdGifts=res.data.holdGifts;
         list.forEach(function(item){
           let originalPrice=item.priceAtg-item.priceDeduction;
           let num=item.goodsSell==null?0:item.goodsSell+item.baseSell==null?0:item.baseSell;
           let handPrice=(item.priceSell-item.priceAtg)-item.priceDeduction;
           item.originalPrice=originalPrice;
           item.handPrice=handPrice;
           item.payNum=num
         })
         that.setData({
          goodsList:list,
          haveORno:res.data.holdGifts  // 0无，1有  礼金包
         })
       }
     })
  },
  userRule(){
    this.setData({
      popupShow:true
    })
  },
  //关闭顶部提示
  close(){
    this.setData({
      showquan:false
    })
  },
  popupClose(){
    this.setData({
      popupShow:false
    })
  }
})